package com.example.hcbmsm.service.impl;

import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.Claim;
import com.example.hcbmsm.domain.*;
import com.example.hcbmsm.mapper.AdminMapper;
import com.example.hcbmsm.mapper.ClMapper;
import com.example.hcbmsm.mapper.DocMapper;
import com.example.hcbmsm.mapper.PatMapper;
import com.example.hcbmsm.service.LoginService;
import com.example.hcbmsm.util.HCBMSMResult;
import com.example.hcbmsm.util.JavaWebToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Xinghm
 * @version 1.0
 * @since 2024/10/29 18:01
 */
@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private PatMapper patMapper;
    @Autowired
    private DocMapper docMapper;
    @Autowired
    private ClMapper clMapper;
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Map patientLogin(String username, String password) {
        PatExample patExample = new PatExample();
        patExample.createCriteria().andPatientUsernameEqualTo(username);
        Pat pat1 = patMapper.selectByExample(patExample).get(0);
        if (pat1 == null) {
            return HCBMSMResult.WARNING(400, "用户名不存在");
        }
        String tempPass = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!pat1.getPatientPassword().equals(tempPass)) {
            return HCBMSMResult.WARNING(400, "用户名或密码错误");
        } else {
            pat1.setPatientPassword("");
            Map map = new HashMap<>();
            map.put("patId", pat1.getPatientId().toString());
            return HCBMSMResult.SUCCESS(JavaWebToken.getToken(map));
        }
    }

    @Override
    public Map checkPatient(String token) {
        try {
            JavaWebToken.checkToken(token);
        }  catch (TokenExpiredException e) {
            System.err.println("token 已经过期 :" + e.getMessage());
            return HCBMSMResult.ERROR("登录已经过期");
        } catch (JWTDecodeException | SignatureVerificationException e){
            return HCBMSMResult.ERROR("校验失败");
        }
        Map<String, Claim> tokenInfo = JavaWebToken.getTokenInfo(token);
        Long patId = new Long(tokenInfo.get("patId").asString());
        Pat pat = patMapper.selectByPrimaryKey(patId);
        pat.setPatientPassword("");
        return HCBMSMResult.SUCCESS(pat);
    }

    @Override
    public Map doctorLogin(String username, String password) {
        DocExample docExample = new DocExample();
        docExample.createCriteria().andDocUsernameEqualTo(username);
        Doc doc1 = docMapper.selectByExample(docExample).get(0);
        if (doc1 == null) {
            return HCBMSMResult.WARNING(400, "用户名不存在");
        }
        String tempPass = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!doc1.getDocPassword().equals(tempPass)) {
            return HCBMSMResult.WARNING(400, "用户名或密码错误");
        } else {
            doc1.setDocPassword("");
            Map map = new HashMap<>();
            map.put("docId", doc1.getDocId().toString());
            Cl cl = new Cl();
            cl.setDocId(doc1.getDocId());
            clMapper.insertSelective(cl);
            return HCBMSMResult.SUCCESS(JavaWebToken.getToken(map));
        }
    }

    @Override
    public Map checkDoctor(String token) {
        try {
            JavaWebToken.checkToken(token);
        }  catch (TokenExpiredException e) {
            System.err.println("token 已经过期 :" + e.getMessage());
            return HCBMSMResult.ERROR("登录已经过期");
        } catch (JWTDecodeException | SignatureVerificationException e){
            return HCBMSMResult.ERROR("校验失败");
        }
        Map<String, Claim> tokenInfo = JavaWebToken.getTokenInfo(token);
        Long patId = new Long(tokenInfo.get("docId").asString());
        Doc doc = docMapper.selectByPrimaryKey(patId);
        doc.setDocPassword("");
        return HCBMSMResult.SUCCESS(doc);
    }

    @Override
    public Map adminLogin(String username, String password) {
        AdminExample adminExample = new AdminExample();
        adminExample.createCriteria().andAdminUsernameEqualTo(username);
        Admin admin1 = adminMapper.selectByExample(adminExample).get(0);
        if (admin1 == null) {
            return HCBMSMResult.WARNING(400, "用户名不存在");
        }
        String tempPass = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!admin1.getAdminPassword().equals(tempPass)) {
            return HCBMSMResult.WARNING(400, "用户名或密码错误");
        } else {
            admin1.setAdminPassword("");
            Map map = new HashMap<>();
            map.put("adminId", admin1.getAdminId().toString());
            return HCBMSMResult.SUCCESS(JavaWebToken.getToken(map));
        }
    }

    @Override
    public Map checkAdmin(String token) {
        try {
            JavaWebToken.checkToken(token);
        }  catch (TokenExpiredException e) {
            System.err.println("token 已经过期 :" + e.getMessage());
            return HCBMSMResult.ERROR("登录已经过期");
        } catch (JWTDecodeException | SignatureVerificationException e){
            return HCBMSMResult.ERROR("校验失败");
        }
        Map<String, Claim> tokenInfo = JavaWebToken.getTokenInfo(token);
        Long patId = new Long(tokenInfo.get("adminId").asString());
        Admin admin = adminMapper.selectByPrimaryKey(patId);
        admin.setAdminPassword("");
        return HCBMSMResult.SUCCESS(admin);
    }
}
